from pathlib import Path
import os
# 
from comfnpy.fnlog import get_time


cwd_path = Path.cwd()
srcfile_folder = cwd_path / Path("./rename/srcfile/")
user_typing = input("Enter your file location: ")
if (len(user_typing) == 0):
    print("[Warning] Nothing commited, using default settings...")
else:
    srcfile_folder = Path(user_typing)
    if (srcfile_folder.exists() == False):
        raise Exception("\n[ERROR] Path you enter is not exist!\n")

output_folder = srcfile_folder.parent / Path("./output/")
if (output_folder.exists() == False):
    os.system("mkdir " + str(output_folder))

report_folder = srcfile_folder.parent / Path("./report/")
if (report_folder.exists() == False):
    os.system("mkdir " + str(report_folder))
report_path = report_folder / Path("./Report_" + get_time(type="fp") + ".txt")


# 
new_suffix = input("Enter suffix of files (.xxx): ")
log_content = ""

for srcfile_path in srcfile_folder.iterdir():
    if (srcfile_path.is_file() == True):        
        newfile_name = srcfile_path.stem + new_suffix    # 利用pathlib库的stem属性，可以无需判断是否已经有后缀
        newfile_path = output_folder / Path(newfile_name)
        os.rename(srcfile_path, newfile_path)
        log_content = "Add/Change suffix: " + srcfile_path.name + "  >>  " + newfile_name
    else:
        log_content = "This path is not file: " + str(srcfile_path)
    print(log_content)

print("[Message] All finished!")
